package system

import (
	"gitee.com/sansaniot/ssiot-core/facade/runtime"
	"gitee.com/sansaniot/ssiot-core/logger"

	"ssnotify/common/constant"
	mq "ssnotify/common/middleware/mqtt"
	"ssnotify/internal/data/action"
	"ssnotify/internal/data/partition"
	"ssnotify/internal/data/thread"
)

func InitSystemBiz() {
	// 注册业务类
	initRegistryPartition()
	// 启动数据线程
	initDataThread()
	// 启动mqtt客户端
	initMqttClient()
}

func initRegistryPartition() {
	// 获取db
	db := runtime.Runtime.GetDbByKey("db")
	if db == nil {
		return
	}
	// ssalarm告警数据接收
	partition.Registry("handle", &action.Handle{Orm: db})

}

func initDataThread() {
	//处理消息通知记录缓存
	thread.NotifyRecordThread{}.Run("NotifyRecordThread")
}

func initMqttClient() {
	mq.MqttClient = mq.NewClient()
	// 固定订阅主题
	mq.MqttClient.InitSubscribe(constant.AlarmNotifyTopic, mq.TopicHandler)
	if _, err := mq.MqttClient.CreateClient(); err != nil {
		logger.Error("mqtt连接失败" + err.Error())
	}
}
